<template>
  <div class="MaterialsReplaceDialog-box">
    <div class="preview-box" ref="previewRef" v-show="store.replaceMaterialsShow && previewShow">
      <img :src="previewUrl" alt="" />
    </div>
    <t-drawer
      v-model:visible="store.replaceMaterialsShow"
      :confirm-on-enter="true"
      :on-cancel="onCancel"
      :on-esc-keydown="onEscKeydown"
      :on-close-btn-click="onCloseBtnClick"
      :on-overlay-click="onOverlayClick"
      :on-close="close"
      :on-confirm="onConfirmAnother"
      style="height: calc(100% - 48px)"
    >
      <div class="sort-box" @click="previewShow = false">
        <div class="heard-box">
          <span>更换素材</span>
          <i class="doorIcon" @click="store.replaceMaterialsShow = false">&#xe63d;</i>
        </div>

        <div class="materials-box">
          <div class="tab-list" v-if="store.materialType === 5">
            <div
              class="tab-solider"
              :class="{
                'tab-solider-1': ActiveType == 'exhibitionStand',
                'tab-solider-2': ActiveType == 'cabinet',
              }"
            ></div>
            <div
              class="tab-item"
              @click="ActiveType = item.type"
              :class="{ 'tab-item-active': item.type === ActiveType }"
              v-for="item in tabList"
              :key="item.type"
            >
              <span>
                {{ item.name }}
              </span>
            </div>
          </div>
          <div class="conten-style-box">
            <p class="ascription-title">本场景素材</p>
            <div class="ascription-list">
              <MaterialsItem
                @click="setActiveIId(item.id)"
                @previewEmit="previewEmit"
                @previewLeave="previewLeave"
                :info="item"
                v-for="item in correlationList"
                v-show="store.materialType !== 5 || (store.materialType === 5 && item.type === ActiveType)"
                :key="item.id"
                :Active="ActiveIId === item.id"
              />
            </div>
            <p class="ascription-title" v-if="NocorrelationList.length > 0">所有素材</p>
            <div class="ascription-list no-ascription-list">
              <MaterialsItem
                v-show="store.materialType !== 5 || (store.materialType === 5 && item.type === ActiveType)"
                @click="setActiveIId(item.id)"
                @previewEmit="previewEmit"
                @previewLeave="previewLeave"
                :info="item"
                v-for="item in NocorrelationList"
                :key="item.id"
                :Active="ActiveIId === item.id"
              />
            </div>
          </div>
        </div>
        <div class="footer-box">
          <button class="set-all" @click="setAll">
            <span>应用到全部</span>
            <t-tooltip>
              <template #content>
                <p class="tooltip-content">
                  点击按钮，当前选中的素材，会应用到场景中所有相同的级别的{{
                    store.materialType === 5 ? '展品' : '大纲'
                  }}。
                </p>
              </template>
              <i class="doorIcon">&#xe635;</i>
            </t-tooltip>
          </button>
        </div>  
      </div>
    </t-drawer>
     <messageModel
    submitText="确认"
    title="提示"
    :visible="setAllShow"
    problem="应用到全部后无法重置"
    message="是否将当前素材应用到全部？"
    @closeClick="setAllShow=false"
    @submitClick="setAllSubmit"
  />
  </div>
</template> 
<script setup lang="ts">
import messageModel from '@/components/messageModel/index.vue';
import { MessagePlugin } from 'tdesign-vue-next';
import { SetspecifyfreeExhibitStyle, restshowpieceListStyle } from '@/material/index';
import { getMOdeListArr } from '@/utils/getRemComputed';
import MaterialsItem from './item.vue';
import { ref, watch, onMounted, nextTick } from 'vue';
import { useProjectStore } from '@/store';
import {
  getmaterialTypeList,
  setMaterialExhibitstyle,
  getExhibitionType,
  AddXNumberExhibitList,
  exportActiveExhibitListInfo,
} from '@/material/index';
const setAllShow=ref(false)
const tabList = ref([
  {
    name: '展板',
    type: 'displayBoard',
  },
  {
    name: '展架',
    type: 'exhibitionStand',
  },
  {
    name: '展柜',
    type: 'cabinet',
  },
]);
const previewRef = ref();
const ActiveType = ref(''); //当前选中的展品素材类型
const store = useProjectStore();
const ActiveIId = ref(0);
const correlationList = ref([]); //关联素材列表
const NocorrelationList = ref([]); //非关联素材列表
const previewUrl = ref('');
const previewShow = ref(false);
const onConfirmAnother = (context) => {
  store.setreplaceMaterialsShow(false);
};
const close = (context) => {
  store.setreplaceMaterialsShow(false);
};
const onCancel = (context) => {};
const onEscKeydown = (context) => {};
const onCloseBtnClick = (context) => {};
const onOverlayClick = (context) => {};
const init = () => {
  let arr = getmaterialTypeList(0, store.materialType);
  correlationList.value = arr.correlationList;
  NocorrelationList.value = arr.NocorrelationList;
  // console.log(arr);
  // ActiveIId
  GetActiveId();
  // console.log();
  if (store.materialType === 5) {
    ActiveType.value = getExhibitionType(ActiveIId.value);
  }
};
const GetActiveId = () => {
  //获取当前
  let idList = getMOdeListArr(store.templateMOdeList, store.contextmenuchildId);
  if (store.selectAuxiliaryIndex === -1) {
    if (store.materialType === 5) {
      if (idList.length === 1) {
        ActiveIId.value = store.templateMOdeList[idList[0]].showpieceList[store.contextmenuGroupId].type;
      } else if (idList.length === 2) {
        ActiveIId.value =
          store.templateMOdeList[idList[0]].children[idList[1]].showpieceList[store.contextmenuGroupId].type;
      } else if (idList.length === 3) {
        ActiveIId.value =
          store.templateMOdeList[idList[0]].children[idList[1]].children[idList[2]].showpieceList[
            store.contextmenuGroupId
          ].type;
      }
    } else {
      if (idList.length === 1) {
        ActiveIId.value = store.templateMOdeList[idList[0]].type;
      } else if (idList.length === 2) {
        ActiveIId.value = store.templateMOdeList[idList[0]].children[idList[1]].type;
      } else if (idList.length === 3) {
        ActiveIId.value = store.templateMOdeList[idList[0]].children[idList[1]].children[idList[2]].type;
      }
    }
  } else {
    if (store.materialType === 5) {
      if (idList.length === 1) {
        ActiveIId.value =
          store.templateMOdeList[idList[0]].auxiliaryList[store.selectAuxiliaryIndex].showpieceList[
            store.contextmenuGroupId
          ].type;
      } else if (idList.length === 2) {
        ActiveIId.value =
          store.templateMOdeList[idList[0]].children[idList[1]].auxiliaryList[store.selectAuxiliaryIndex].showpieceList[
            store.contextmenuGroupId
          ].type;
      } else if (idList.length === 3) {
        ActiveIId.value =
          store.templateMOdeList[idList[0]].children[idList[1]].children[idList[2]].auxiliaryList[
            store.selectAuxiliaryIndex
          ].showpieceList[store.contextmenuGroupId].type;
      }
    } else {
      if (idList.length === 1) {
        ActiveIId.value = store.templateMOdeList[idList[0]].auxiliaryList[store.selectAuxiliaryIndex].type;
      } else if (idList.length === 2) {
        ActiveIId.value =
          store.templateMOdeList[idList[0]].children[idList[1]].auxiliaryList[store.selectAuxiliaryIndex].type;
      } else if (idList.length === 3) {
        ActiveIId.value =
          store.templateMOdeList[idList[0]].children[idList[1]].children[idList[2]].auxiliaryList[
            store.selectAuxiliaryIndex
          ].type;
      }
    }
  }
};
const previewEmit = (e: string, heights: number) => {
  previewRef.value.style.top = heights + 'px';
  previewUrl.value = e;
  previewShow.value = true;
};
const previewLeave = () => {
  previewShow.value = false;
};
const setActiveIId = (id) => {
  setTimeout(() => {
    ActiveIId.value = id;
    let idList = getMOdeListArr(store.templateMOdeList, store.contextmenuchildId);
    if (store.selectAuxiliaryIndex === -1) {
      if (store.materialType === 5) {
        if (idList.length === 1) {
          let ActiveInfo = exportActiveExhibitListInfo(ActiveIId.value); //获取当前
          let oldWidth = store.templateMOdeList[idList[0]].showpieceList[store.contextmenuGroupId].width; //获取原来宽度

          store.templateMOdeList[idList[0]].showpieceList[store.contextmenuGroupId] = setMaterialExhibitstyle(
            store.templateMOdeList[idList[0]].showpieceList[store.contextmenuGroupId],
            ActiveIId.value,
          );
          if (ActiveInfo.type !== 'cabinet') {
            ///如果不是展柜的话进行伸展操作
            store.templateMOdeList[idList[0]].showpieceList = AddXNumberExhibitList(
              store.templateMOdeList[idList[0]].showpieceList,
              store.contextmenuGroupId,
              store.templateMOdeList[idList[0]].showpieceList[store.contextmenuGroupId].width - oldWidth,
            );
          }
        } else if (idList.length === 2) {
          let ActiveInfo = exportActiveExhibitListInfo(ActiveIId.value); //获取当前
          let oldWidth =
            store.templateMOdeList[idList[0]].children[idList[1]].showpieceList[store.contextmenuGroupId].width; //获取原来宽度
          store.templateMOdeList[idList[0]].children[idList[1]].showpieceList[store.contextmenuGroupId] =
            setMaterialExhibitstyle(
              store.templateMOdeList[idList[0]].children[idList[1]].showpieceList[store.contextmenuGroupId],
              ActiveIId.value,
            );
          if (ActiveInfo.type !== 'cabinet') {
            ///如果不是展柜的话进行伸展操作
            store.templateMOdeList[idList[0]].children[idList[1]].showpieceList = AddXNumberExhibitList(
              store.templateMOdeList[idList[0]].children[idList[1]].showpieceList,
              store.contextmenuGroupId,
              store.templateMOdeList[idList[0]].children[idList[1]].showpieceList[store.contextmenuGroupId].width -
                oldWidth,
            );
          }
        } else if (idList.length === 3) {
          let ActiveInfo = exportActiveExhibitListInfo(ActiveIId.value); //获取当前
          let oldWidth =
            store.templateMOdeList[idList[0]].children[idList[1]].children[idList[2]].showpieceList[
              store.contextmenuGroupId
            ].width; //获取原来宽度
          store.templateMOdeList[idList[0]].children[idList[1]].children[idList[2]].showpieceList[
            store.contextmenuGroupId
          ] = setMaterialExhibitstyle(
            store.templateMOdeList[idList[0]].children[idList[1]].children[idList[2]].showpieceList[
              store.contextmenuGroupId
            ],
            ActiveIId.value,
          );
          if (ActiveInfo.type !== 'cabinet') {
            ///如果不是展柜的话进行伸展操作
            store.templateMOdeList[idList[0]].children[idList[1]].children[idList[2]].showpieceList =
              AddXNumberExhibitList(
                store.templateMOdeList[idList[0]].children[idList[1]].children[idList[2]].showpieceList,
                store.contextmenuGroupId,
                store.templateMOdeList[idList[0]].children[idList[1]].children[idList[2]].showpieceList[
                  store.contextmenuGroupId
                ].width - oldWidth,
              );
          }
        }
      } else {
        if (idList.length === 1) {
          store.templateMOdeList[idList[0]].type = ActiveIId.value;
        } else if (idList.length === 2) {
          store.templateMOdeList[idList[0]].children[idList[1]].type = ActiveIId.value;
        } else if (idList.length === 3) {
          store.templateMOdeList[idList[0]].children[idList[1]].children[idList[2]].type = ActiveIId.value;
        }
      }
    } else {
      if (store.materialType === 5) {
        if (idList.length === 1) {
          let ActiveInfo = exportActiveExhibitListInfo(ActiveIId.value); //获取当前
          let oldWidth =
            store.templateMOdeList[idList[0]].auxiliaryList[store.selectAuxiliaryIndex].showpieceList[
              store.contextmenuGroupId
            ].width; //获取原来宽度
          store.templateMOdeList[idList[0]].auxiliaryList[store.selectAuxiliaryIndex].showpieceList[
            store.contextmenuGroupId
          ] = setMaterialExhibitstyle(
            store.templateMOdeList[idList[0]].auxiliaryList[store.selectAuxiliaryIndex].showpieceList[
              store.contextmenuGroupId
            ],
            ActiveIId.value,
          );
          if (ActiveInfo.type !== 'cabinet') {
            ///如果不是展柜的话进行伸展操作
            store.templateMOdeList[idList[0]].auxiliaryList[store.selectAuxiliaryIndex].showpieceList =
              AddXNumberExhibitList(
                store.templateMOdeList[idList[0]].auxiliaryList[store.selectAuxiliaryIndex].showpieceList,
                store.contextmenuGroupId,
                store.templateMOdeList[idList[0]].auxiliaryList[store.selectAuxiliaryIndex].showpieceList[
                  store.contextmenuGroupId
                ].width - oldWidth,
              );
          }
        } else if (idList.length === 2) {
          let ActiveInfo = exportActiveExhibitListInfo(ActiveIId.value); //获取当前
          let oldWidth =
            store.templateMOdeList[idList[0]].children[idList[1]].auxiliaryList[store.selectAuxiliaryIndex]
              .showpieceList[store.contextmenuGroupId].width; //获取原来宽度
          store.templateMOdeList[idList[0]].children[idList[1]].auxiliaryList[store.selectAuxiliaryIndex].showpieceList[
            store.contextmenuGroupId
          ] = setMaterialExhibitstyle(
            store.templateMOdeList[idList[0]].children[idList[1]].auxiliaryList[store.selectAuxiliaryIndex]
              .showpieceList[store.contextmenuGroupId],
            ActiveIId.value,
          );
          if (ActiveInfo.type !== 'cabinet') {
            ///如果不是展柜的话进行伸展操作
            store.templateMOdeList[idList[0]].children[idList[1]].auxiliaryList[
              store.selectAuxiliaryIndex
            ].showpieceList = AddXNumberExhibitList(
              store.templateMOdeList[idList[0]].children[idList[1]].auxiliaryList[store.selectAuxiliaryIndex]
                .showpieceList,
              store.contextmenuGroupId,
              store.templateMOdeList[idList[0]].children[idList[1]].auxiliaryList[store.selectAuxiliaryIndex]
                .showpieceList[store.contextmenuGroupId].width - oldWidth,
            );
          }
        } else if (idList.length === 3) {
          let ActiveInfo = exportActiveExhibitListInfo(ActiveIId.value); //获取当前
          let oldWidth =
            store.templateMOdeList[idList[0]].children[idList[1]].children[idList[2]].auxiliaryList[
              store.selectAuxiliaryIndex
            ].showpieceList[store.contextmenuGroupId].width; //获取原来宽度
          store.templateMOdeList[idList[0]].children[idList[1]].children[idList[2]].auxiliaryList[
            store.selectAuxiliaryIndex
          ].showpieceList[store.contextmenuGroupId] = setMaterialExhibitstyle(
            store.templateMOdeList[idList[0]].children[idList[1]].children[idList[2]].auxiliaryList[
              store.selectAuxiliaryIndex
            ].showpieceList[store.contextmenuGroupId],
            ActiveIId.value,
          );
          if (ActiveInfo.type !== 'cabinet') {
            ///如果不是展柜的话进行伸展操作
            store.templateMOdeList[idList[0]].children[idList[1]].children[idList[2]].auxiliaryList[
              store.selectAuxiliaryIndex
            ].showpieceList = AddXNumberExhibitList(
              store.templateMOdeList[idList[0]].children[idList[1]].children[idList[2]].auxiliaryList[
                store.selectAuxiliaryIndex
              ].showpieceList,
              store.contextmenuGroupId,
              store.templateMOdeList[idList[0]].children[idList[1]].children[idList[2]].auxiliaryList[
                store.selectAuxiliaryIndex
              ].showpieceList[store.contextmenuGroupId].width - oldWidth,
            );
          }
        }
      } else {
        if (idList.length === 1) {
          store.templateMOdeList[idList[0]].auxiliaryList[store.selectAuxiliaryIndex].type = ActiveIId.value;
        } else if (idList.length === 2) {
          store.templateMOdeList[idList[0]].children[idList[1]].auxiliaryList[store.selectAuxiliaryIndex].type =
            ActiveIId.value;
        } else if (idList.length === 3) {
          store.templateMOdeList[idList[0]].children[idList[1]].children[idList[2]].auxiliaryList[
            store.selectAuxiliaryIndex
          ].type = ActiveIId.value;
        }
      }
    }
    nextTick(() => {
      store.settingHistoryList();
      store.SaveFreeInfoData();
    });
  }, 10);
};
const setAll = () => {
  //应用所有类型
setAllShow.value=true
  
};
const  setAllSubmit=()=>{
setTimeout(() => {
    if (store.materialType === 5) {
      let typeInfo = exportActiveExhibitListInfo(ActiveIId.value); //获取type详情信息
      if (typeInfo.type !== 'cabinet') {
        //如果选择类型是展品且应用的类型不是展柜则重置所有展品的数值
        store.templateMOdeList.forEach((level1, index1) => {
          store.templateMOdeList[index1].showpieceList = restshowpieceListStyle(level1.showpieceList, ActiveIId.value);
          level1.auxiliaryList.forEach((element, auxiliaryIndex) => {
            store.templateMOdeList[index1].auxiliaryList[auxiliaryIndex].showpieceList = restshowpieceListStyle(
              element.showpieceList,
              ActiveIId.value,
            );
          });
          level1.children.forEach((level2, index2) => {
            level2.auxiliaryList.forEach((element, auxiliaryIndex) => {
              store.templateMOdeList[index1].children[index2].auxiliaryList[auxiliaryIndex].showpieceList =
                restshowpieceListStyle(element.showpieceList, ActiveIId.value);
            });
            store.templateMOdeList[index1].children[index2].showpieceList = restshowpieceListStyle(
              level2.showpieceList,
              ActiveIId.value,
            );
            level2.children.forEach((level3, index3) => {
              level3.auxiliaryList.forEach((element, auxiliaryIndex) => {
                store.templateMOdeList[index1].children[index2].children[index3].auxiliaryList[
                  auxiliaryIndex
                ].showpieceList = restshowpieceListStyle(element.showpieceList, ActiveIId.value);
              });
              store.templateMOdeList[index1].children[index2].children[index3].showpieceList = restshowpieceListStyle(
                level3.showpieceList,
                ActiveIId.value,
              );
            });
          });
        });
      }
    }
    let newList = SetspecifyfreeExhibitStyle(store.templateMOdeList, store.materialType, ActiveIId.value);
    store.templateMOdeList = newList;

    nextTick(() => {
      store.settingHistoryList();
      store.SaveFreeInfoData();
      MessagePlugin.success('设置成功');
      setAllShow.value=false
    });
  }, 2);
}
watch(
  () => store.replaceMaterialsShow,
  () => {
    if (store.replaceMaterialsShow) {
      init();
    } else {
      previewShow.value = false;
    }
  },
  { deep: true },
);
</script>

<style scoped lang="less">
.sort-box {
  position: relative;
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
}
:deep(.t-drawer__header) {
  display: none;
}
:deep(.t-drawer__footer) {
  display: none;
}
:deep(.title1){
  .doorIcon{
    // display: none;
  }
}
:deep(.t-drawer__body) {
  padding: 0px;
  overflow: visible;
}
:deep(.t-drawer__content-wrapper) {
  width: 348px !important;
}
// :deep(.t-drawer--open) {
//   height: calc(100% - 34px);
// }
:deep(.t-drawer--right) {
  top: auto !important;
  bottom: 0px !important;
}
.heard-box {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  height: 48px;
  background: #f5f5f5;
  padding: 0 24px;
  span {
    font-family: PingFangSC, PingFang SC;
    font-weight: 400;
    font-size: 18px;
    color: rgba(0, 0, 0, 0.59);
    line-height: 28px;
  }
  .doorIcon {
    cursor: pointer;
    font-size: 16px;
    color: rgba(0, 0, 0, 0.59);
  }
}

.footer-box {
  display: flex;
  justify-content: flex-end;
  width: 100%;
  height: 48px;
  padding: 8px 24px;
  .set-all {
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 32px;
    border-radius: 4px;
    border: 1px solid #d9d9d9;
    background: transparent;
    span {
      font-family: PingFangSC, PingFang SC;
      font-weight: 400;
      font-size: 16px;
      color: rgba(0, 0, 0, 0.8);
      line-height: 24px;
    }
    .doorIcon {
      font-size: 16px;
      color: rgba(0, 0, 0, 0.59);
      margin-left: 4px;
    }
  }
}
.materials-box {
  display: flex;
  flex-direction: column;
  padding: 0px 24px;

  width: 100%;
  height: calc(100vh - 140px);
  // overflow: auto;
  scrollbar-width: none;
}
.conten-style-box {
  width: 100%;
  display: flex;
  flex-direction: column;
  flex: 1;
  overflow: auto;
  scrollbar-width: none;
}
.ascription-title {
  margin-top: 16px;
  font-family: PingFangSC, PingFang SC;
  font-weight: 500;
  font-size: 16px;
  color: rgba(0, 0, 0, 0.8);
  line-height: 24px;
}
.ascription-list {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  width: 100%;
  box-shadow: inset 0px -1px 0px 0px #f0f0f0;
  padding-bottom: 16px;
}
.no-ascription-list {
  box-shadow: none;
  padding-bottom: 16px;
}
.preview-box {
  display: flex;
  align-items: center;
  justify-content: center;
  position: fixed;
  top: calc(50% - 160px);
  right: 340px;
  width: 320px;
  height: 320px;
  background: #fff;
  box-shadow: 0px 12px 12px -7px rgba(0, 0, 0, 0.1), 0px 19px 24px 2px rgba(0, 0, 0, 0.06),
    0px 7px 33px 5px rgba(0, 0, 0, 0.05);
  border-radius: 4px;
  border: 1px solid #e6e6e6;
  z-index: 99999;
  img {
    object-fit: contain;
    max-width: 100%;
    max-height: 100%;
    width: auto;
    height: auto;
  }
}
.tab-list {
  position: relative;
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  height: 48px;
  box-shadow: inset 0px -1px 0px 0px #f0f0f0;
}
.tab-item {
  cursor: pointer;
  font-family: PingFangSC, PingFang SC;
  font-weight: 400;
  font-size: 16px;
  color: rgba(0, 0, 0, 0.59);
  line-height: 24px;
  width: 32px;
}
.tab-item-active {
  color: #e62b17;
}
.tab-solider {
  transition: all 0.3s;
  position: absolute;
  bottom: -1px;
  left: 0;
  width: 32px;
  height: 2px;
  background: #e62b17;
  border-radius: 1px;
}
.tab-solider-1 {
  left: 50%;
  transform: translateX(-50%);
}
.tab-solider-2 {
  left: 100%;
  transform: translateX(-100%);
}
.tooltip-content {
  width: 198px;
}
:deep(.t-drawer__mask) {
  background: transparent;
}
</style>